<?php
/**custom comments**/
function custom_comments($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;
	$GLOBALS['comment_depth'] = $depth;
	global $commentcount;
	if(!$commentcount) {
		$commentcount = 0;
	}
?>
	<li id="comment-<?php comment_ID(); ?>" class="<?php eachBlue_comment_class(); ?>">
	 <dl>
      <dt>
	    <?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 24); } ?>
	 </dt>
	 <dd>
	   <h3>     
		 <cite>
		  <?php eachBlue_comment_author(); ?>
		</cite>
		   <?php
				if((get_option('thread_comments')) && ($args['type'] == 'all' || get_comment_type() == 'comment')) :
					$max_depth = get_option('thread_comments_depth');
					echo comment_reply_link(array(
						'reply_text' => __('Reply','eachBlue'), 
						'login_text' => __('Login to reply.','eachBlue'),
						'depth' => $depth,
						'max_depth' => $max_depth, 
						'before' => '<span class="separator">|</span> <span class="comment-reply-link">', 
						'after' => '</span>'
					));
				endif;
			?>		
			<?php edit_comment_link('<span class="edit">'.__('Edit','eachBlue').'</span>',' <span class="separator">|</span> ',''); ?>
			<span class="num"><?php printf(++$commentcount); ?><sup>F</sup></span>
	   </h3>
	   <h4><?php printf( __('%1$s at %2$s', 'eachBlue'), get_comment_date(__('F jS, Y', 'eachBlue')), get_comment_time(__('H:i', 'eachBlue')) ); ?></h4>
	 </dd>
	</dl>
	<div class="commenttext">
	     <?php if ($comment->comment_approved == '0') : ?>
		 <p class="msgtip"><small><?php _e('Your comment is awaiting moderation.', 'eachBlue'); ?></small></p>
		 <?php endif; ?>
		 <div id="commentbody-<?php comment_ID() ?>"><?php comment_text() ?></div>
	</div>
<?php
}

function custom_comments_end() {
	echo '</li>';
}

function eachBlue_comment_class() {
	global $comment;
	static $comment_alt;
	$classes = array();

	if(function_exists('get_comment_class'))
		$classes = get_comment_class();

	$classes[] = get_comment_type();;

	if($comment->user_id > 0 && $user = get_userdata($comment->user_id)) :

		$classes[] = 'user user-' . $user->user_nicename;

		if($post = get_post($post_id)) :
			if($comment->user_id === $post->post_author)
				$classes[] = 'author author-' . $user->user_nicename;
		endif;
	else :
		$classes[] = 'reader';
	endif;

	if($comment_alt++ % 2) :
		$classes[] = 'even';
		$classes[] = 'alt';
	else :
		$classes[] = 'odd';
	endif;

	$email = get_comment_author_email();
	$url = get_comment_author_url();
	if(!empty($email) && !empty($url)) {
		$microid = 'microid-mailto+http:sha1:' . sha1(sha1('mailto:'.$email).sha1($url));
		$classes[] = $microid;
	}


	$classes = join(' ', $classes);

	echo $classes;
}

function eachBlue_comment_author() {
	global $comment;

	$author = get_comment_author();
	$url = get_comment_author_url();

	if($url == 'http://')
		$url = false;

	if($comment->user_id > 0) :
		$user = get_userdata($comment->user_id);
		if($user->display_name)
			$author = $user->display_name;
		elseif($user->user_nickname)
			$author = $user->nickname;
		elseif($user->user_nicename)
			$author = $user->user_nicename;
		else
			$author = $user->user_login;
	endif;

	if($url) :
		$output = '<cite title="' . $url . '">';
		$output .= '<a href="' . $url . '" title="' . $author . '" class="external nofollow">' . $author . '</a>';
		$output .= '</cite>';
	else :
		$output = '<cite>';
		$output .= $author;
		$output .= '</cite>';
	endif;

	echo $output;
}

function cache_recent_comments(){
	$cached = get_option('cache_recent_comments');
	if($cached){
		echo $cached; 
	}else{
		$cached = cache_get_recent_comments();
		//echo "Cache Updated";
		echo $cached;
	}
}
function cache_get_recent_comments() {
	$before = '<li> ';
	$after = '</li>';
	global $wpdb;
	$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
    comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    comment_type,comment_author_url,
    SUBSTRING(comment_content,1,40) AS com_excerpt
    FROM $wpdb->comments
    LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    $wpdb->posts.ID)
    WHERE comment_approved = '1' AND comment_type = '' AND
    post_password = ''
    AND comment_author != 'admin'
    AND comment_author != 'admin'
    ORDER BY comment_date_gmt DESC
    LIMIT 10";
	$comments = $wpdb->get_results($sql);
	$output = '';
	foreach ($comments as $comment) {
		$comment_author = strip_tags($comment->comment_author);
		$comment_content = strip_tags($comment->com_excerpt);
		$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
		$post_title = $comment->post_title;
		$output .= $before ."<font color=#0A9BDC>". $comment_author."</font>:" .'<a href="' . $permalink . '" title="on ' . $post_title . '">'. $comment_content .'</a>' . $after;
	}
	$output = '<ul>'.$output.'</ul>';
	update_option('cache_recent_comments', $output);
	return $output;
}
function cache_popular_posts(){
	$cached = get_option('cache_popular_posts');
	if($cached){
		echo $cached; 
	}else{
		$cached = cache_get_popular_posts();
		echo "Cache Updated";
		echo $cached;
	}
}
function cache_get_popular_posts(){
	global $wpdb;
	$sql = "SELECT comment_count,ID,post_date,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10";
    $result = $wpdb->get_results($sql);  
    foreach ($result as $topten){
		$postid = $topten->ID;
		$postlink = get_permalink($postid);
        $title = $topten->post_title;
        $commentcount = $topten->comment_count;
        $posttime= date("Y.m.d",strtotime($topten->post_date));
        if ($commentcount != 0){
			$output .= "<li>$posttime -<a href=\"$postlink\">$title</a> ($commentcount)</li>";
		}
	}
	$output = '<ul>'.$output.'</ul>';
	update_option('cache_popular_posts', $output); 
	return $output;
}
function cache_recent_posts(){
	$cached = get_option('cache_recent_posts');
	if($cached){
		echo $cached;
	}else{
		$cached = cache_get_recent_posts();
		//echo "Update cache";
		echo $cached;
	}
}
	function cache_get_recent_posts(){
		$limit = 10;
		$before = '<li>';
		$after = '</li>'; 
		$show_pass_post = false;
		global $wpdb;
	    $sql = "SELECT ID, post_title, comment_count,post_date, post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'";
		if(!$show_pass_post) $sql .= "AND post_password ='' ";
		$sql .= "ORDER BY post_date DESC LIMIT $limit";
	    $posts = $wpdb->get_results($sql);
		$output = '';
		if ($output) {
			return "error";
		}
	    foreach ($posts as $post) {
			$post_title = stripslashes($post->post_title);
			$post_date = mysql2date('j.m.Y', $post->post_date);
			$permalink = get_permalink($post->ID);
			$post_content = strip_tags($post->post_content); 			
			$output .= $before . '<a href="' . $permalink . '" title="'.$post_title.'">';
			$output .= $post_title;
			$output .= '</a>'.$after;
		}
		update_option('cache_recent_posts', $output);
		return $output;	
	}
add_action('publish_post', 'cache_get_popular_posts');
add_action('comment_post', 'cache_get_recent_comments');
add_action('edit_comment', 'cache_get_recent_comments');
add_action('publish_post', 'cache_get_recent_posts');
?>
<?php
/** eachBlue options */
class eachBlueOptions {
	function getOptions() {
		$options = get_option('eachBlue_options');
		if (!is_array($options)) {
			$options['notice'] = true;
			$options['notice_content'] = '';
			$options['description'] = '';
			$options['keywords'] = '';
			$options['author'] = '';
			$options['google_cse'] = false;
			$options['google_cse_cx'] = '';
			$options['footer_insert'] = '';
			update_option('eachBlue_options', $options);
		}
		return $options;
	}

	function add() {
		if(isset($_POST['eachBlue_save'])) {
			$options = eachBlueOptions::getOptions();

			// notice
			if ($_POST['notice']) {
				$options['notice'] = (bool)true;
			} else {
				$options['notice'] = (bool)false;
			}
			$options['notice_content'] = stripslashes($_POST['notice_content']);
			// meta seo
			$options['description'] = stripslashes($_POST['description']);
			$options['keywords'] = stripslashes($_POST['keywords']);
			$options['author'] = stripslashes($_POST['author']);
			// google custom search engine
			if ($_POST['google_cse']) {
				$options['google_cse'] = (bool)true;
			} else {
				$options['google_cse'] = (bool)false;
			}
			$options['google_cse_cx'] = stripslashes($_POST['google_cse_cx']);	
			// meta seo	
			$options['footer_insert'] = stripslashes($_POST['footer_insert']);

			update_option('eachBlue_options', $options);

		} else {
			eachBlueOptions::getOptions();
		}

		add_theme_page("eachBlue Options", "eachBlue Options", 'edit_themes', basename(__FILE__), array('eachBlueOptions', 'display'));
	}
	function display() {
		$options = eachBlueOptions::getOptions();

?>	
<?php
// Theme data
	$theme_data = get_theme_data(TEMPLATEPATH . '/style.css');
?>

<div id="poststuff" class="dlm">
<form action="#" method="post" enctype="multipart/form-data" name="eachBlue_form" id="eachBlue_form">
	<div class="wrap">
		<h2><?php _e('eachBlue Options', 'eachBlue'); ?></h2>
		
	<div class="postbox close">
		<h3><?php _e('About This Theme','eachBlue'); ?></h3>	
<div class="inside">

		<table class="form-table">
		<tr>
		<th><?php _e('Theme Description:','eachBlue'); ?></th>
		<td><?php echo $theme_data['Description']; ?></td>
	</tr>
	<tr>
		<th><?php _e('Theme Version:','eachBlue'); ?></th>
		<td><?php echo $theme_data['Title']; ?> <?php echo $theme_data['Version']; ?></td>
	</tr>
	<tr>
		<th><?php _e('Theme Documentation:','eachBlue'); ?></th>
		<td><a href="<?php echo $theme_data['URI']; ?>" title="<?php _e('Theme Documentation','eachBlue'); ?>"><?php _e('Theme Documentation','eachBlue'); ?></a></td>
	</tr>
	<tr>
		<th><?php _e('Theme Support:','eachBlue'); ?></th>
		<td><a href="http://eachsite.org/eachblue-v1/" title="<?php _e('Get support for this theme','eachBlue'); ?>"><?php _e('Support me.','eachBlue'); ?></a></td>
	</tr>
		</table>
</div>
</div>

<div class="postbox open">
	<h3><?php _e('Select your theme notice','eachBlue'); ?></h3>
	<div class="inside">
		<table class="form-table">
			<tbody>
				<tr valign="top">
					<th scope="row">
						<?php _e('Notice', 'eachBlue'); ?>
						<br/>
						<small style="font-weight:normal;"><?php _e('HTML enabled', 'eachBlue') ?></small>
					</th>
					<td>
						<label>
							<input name="notice" type="checkbox" value="checkbox" <?php if($options['notice']) echo "checked='checked'"; ?> />
							 <?php _e('Show notice.', 'eachBlue'); ?>
						</label>
						</div>
						<label>
							<textarea name="notice_content" cols="50" rows="10" id="notice_content" style="width:98%;font-size:12px;" class="code"><?php echo($options['notice_content']); ?></textarea>
						</label>
					</td>
				</tr>
			</tbody>
		</table>
		<table class="form-table">
			<tbody>
				<tr valign="top">
					<th scope="row"><?php _e('Meta', 'eachBlue'); ?></th>
					<td>
						<?php _e('Keywords:', 'eachBlue'); ?> <small><?php _e('( Separate keywords with commas )', 'eachBlue'); ?></small>
						<br/>
						<input type="text" name="keywords" id="keywords" class="code" style="width:98%;" value="<?php echo($options['keywords']); ?>">
						<br/>
						<?php _e('Description:', 'eachBlue'); ?>
						<br/>
						<input type="text" name="description" id="description" class="code" style="width:98%;" value="<?php echo($options['description']); ?>">
						<br/>
						<?php _e('Author:', 'eachBlue'); ?> <?php _e('( Separate author with commas )', 'eachBlue'); ?></small>
						<br/>
						<input type="text" name="author" id="author" class="code" style="width:98%;" value="<?php echo($options['author']); ?>">	
					</td>
				</tr>
			</tbody>
		</table>
		<table class="form-table">
			<tbody>
				<tr valign="top">
					<th scope="row"><?php _e('Search', 'eachBlue'); ?></th>
					<td>
						<label>
							<input name="google_cse" type="checkbox" value="checkbox" <?php if($options['google_cse']) echo "checked='checked'"; ?> />
							 <?php _e('Using google custom search engine.', 'eachBlue'); ?>
						</label>
						<br/>
						<?php _e('CX:', 'eachBlue'); ?>
						 <input type="text" name="google_cse_cx" id="google_cse_cx" class="code" size="40" value="<?php echo($options['google_cse_cx']); ?>">
						<br/>
						<?php _e('Find <code>name="cx"</code> in the <strong>Search box code</strong> of <a href="http://www.google.com/coop/cse/">Google Custom Search Engine</a>, and type the <code>value</code> here.<br/>For example: <code>016923013645410625752:v78cazfpyyu</code>', 'eachBlue'); ?>
					</td>
				</tr>
			</tbody>
		</table>
		<table class="form-table">
			<tbody>
				<tr valign="top">
					<th scope="row"><?php _e('Footer Insert:', 'eachBlue'); ?></th>
					<td>
						<small><?php _e('You can place Google Analytics JavaScript here to have it inserted automatically into your theme.', 'eachBlue'); ?></small>
						<br/>
						<label>
							<textarea name="footer_insert" id="footer_insert" cols="50" rows="10" style="width:98%;font-size:12px;" class="code"><?php echo($options['footer_insert']); ?></textarea>
						</label>	
					</td>
				</tr>
			</tbody>
		</table>

		<p class="submit">
			<input type="submit" name="eachBlue_save" value="<?php _e('Save Changes', 'eachBlue'); ?>" />
		</p>
</div>
</div>

</form>
</div>
<?php
	}
}
// Register functions
add_action('admin_menu', array('eachBlueOptions', 'add'));
/** l10n */
function theme_init(){
	load_theme_textdomain('eachBlue');
}
add_action ('init', 'theme_init');
// Widgets
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '<div class="sect">',
        'after_widget' => '</div>',
        'before_title' => '<h2>',
        'after_title' => '</h2>',
    ));
?>